package com.loveazure.ui;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.loveazure.bll.Handler;
import com.loveazure.eo.User;

public class LoginPanel extends IPanel {

	public LoginPanel() {
		super();
	}

	private JButton loginBtn;
	private JButton newBtn;
	private JButton delBtn;
	private JComboBox userlist;
	private JLabel info;

	@Override
	public void iComponent() {
		userlist = new JComboBox();
		userlist.setEditable(true);
		userlist.setPreferredSize(new Dimension(70, 26));

		newBtn = new JButton("添加");
		newBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!(userlist.getSelectedItem() instanceof String)) {
					return;
				}
				String input = (String) userlist.getSelectedItem();

				if (input == null || input.trim().equals("")) {
					info.setText("请输入帐号");
					return;
				}
				User user = new User();
				user.setName(input);
				if (!Handler.getInstance().getOper().addUser(user)) {
					info.setText("该帐号已存在");
					return;
				}
				userlist.addItem(user);
				userlist.setSelectedItem(user);
				info.setText("添加成功");
			}
		});
		delBtn = new JButton("删除");
		delBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (userlist.getSelectedItem() == null) {
					info.setText("请选择要删除的帐号");
					return;
				}
				if (!(userlist.getSelectedItem() instanceof User)) {
					return;
				}
				int result = JOptionPane.showConfirmDialog(null, "确认删除?", "",
						JOptionPane.YES_NO_OPTION);
				if (result != 0) {
					return;
				}
				if (!Handler.getInstance().getOper().deleteUser(
						(User) userlist.getSelectedItem())) {
					info.setText("无法删除");
					return;
				} else {
					info.setText("成功删除帐号 " + userlist.getSelectedItem());
				}
				userlist.removeItemAt(userlist.getSelectedIndex());

			}
		});
		loginBtn = new JButton("登录");
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (userlist.getSelectedItem() instanceof User) {
					Handler.getInstance().getOper().login(
							(User) userlist.getSelectedItem());
				} else {
					info.setText("登录出错");
				}

			}
		});
		info = new JLabel();

		JLabel title = new JLabel("智能单词记忆工具");

		add(title, new GBC(0, 0, 4, 1));
		add(new JLabel("帐号："), new GBC(0, 1, 1, 1));
		add(userlist, new GBC(1, 1, 2, 1).setFill(GBC.HORIZONTAL));
		add(newBtn, new GBC(0, 2, 2, 1));
		add(delBtn, new GBC(2, 2, 2, 1));
		add(loginBtn, new GBC(0, 3, 4, 1));
		add(info, new GBC(0, 5, 4, 1));
	}

	@Override
	public void iLayout() {
		GridBagLayout gbl = new GridBagLayout();
		
		gbl.columnWidths = new int[] { 40, 80, 60, 60 };
		gbl.rowHeights = new int[] { 50, 50, 50, 50, 50, 50 };
		
		setLayout(gbl);
	}

	@Override
	public void iInit() {
		User[] users = Handler.getInstance().getOper().getAllUsers();
		if (users != null) {
			for (User user : users) {
				this.userlist.addItem(user);
			}
		}
	}
}
